<template>
  <div class="profile-container">
    <NavigationBar />
    <div class="profile-content">
      <h1 class="profile-title">🐾 个人中心</h1>
      
      <div class="profile-card">
        <!-- 用户信息 -->
        <div class="user-info">
          <div class="info-item">
            <span class="info-label">昵称：</span>
            <span class="info-value">{{ userProfile.username }}</span>
          </div>
          <div class="info-item">
            <span class="info-label">邮箱：</span>
            <span class="info-value">{{ userProfile.email }}</span>
          </div>
          <div class="info-item">
            <span class="info-label">电话：</span>
            <span class="info-value">{{ userProfile.phone || '未填写' }}</span>
          </div>
          <div class="info-item">
            <span class="info-label">地址：</span>
            <span class="info-value">{{ userProfile.address || '未填写' }}</span>
          </div>
        </div>

        <!-- 操作按钮 -->
        <div class="action-buttons">
          <button @click="editProfile" class="edit-btn">
            <i class="fas fa-edit"></i> 编辑资料
          </button>
          <button @click="logout" class="logout-btn">
            <i class="fas fa-sign-out-alt"></i> 退出登录
          </button>
        </div>
      </div>
    </div>
    <Footer />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, onBeforeRouteUpdate } from 'vue-router'
import axios from 'axios'; // Vite 会自动处理依赖import 
import NavigationBar from '@/components/NavigationBar.vue'
import Footer from '@/components/Footer.vue'

const router = useRouter()

// 用户数据
const userProfile = ref({
  username: '加载中...',
  email: '加载中...',
  phone: '',
  address: ''
})

// 获取用户信息
const fetchUserProfile = async () => {
  const token = localStorage.getItem('token')
  if (!token) {
    router.push('/')
    return
  }

  try {
    const response = await axios.get('http://localhost:3000/profile', {
      headers: { Authorization: token }
    })
    userProfile.value = response.data.data
  } catch (error) {
    console.error('获取用户信息失败:', error)
    alert('获取信息失败，请重新登录')
    router.push('/')
  }
}

// 编辑资料
const editProfile = () => {
  router.push('/profile/edit')
}

// 退出登录
const logout = () => {
  localStorage.removeItem('token')
  router.push('/')
}

onBeforeRouteUpdate(() => {
  fetchUserProfile()
})

onMounted(() => {
  fetchUserProfile()
})
</script>

<style scoped>
/* 样式保持不变，只需移除与头像相关的样式 */
.profile-container {
  background-color: #fffaf5;
  min-height: 100vh;
  padding: 2rem 1rem;
}

.profile-content {
  max-width: 800px;
  margin: 0 auto;
}

.profile-title {
  color: #8b7355;
  text-align: center;
  font-size: 2.2rem;
  margin-bottom: 2rem;
  padding-bottom: 1rem;
  border-bottom: 2px solid #f0e2d4;
}

.profile-card {
  background: white;
  border-radius: 15px;
  padding: 2rem;
  box-shadow: 0 4px 12px rgba(0,0,0,0.06);
}

.user-info {
  margin: 2rem 0;
}

.info-item {
  display: flex;
  align-items: center;
  margin-bottom: 1.2rem;
  padding: 0.8rem;
  background: #fbf7f2;
  border-radius: 8px;
}

.info-label {
  color: #7f6b5e;
  font-weight: 500;
  min-width: 80px;
}

.info-value {
  color: #635347;
  flex-grow: 1;
}

.action-buttons {
  display: flex;
  gap: 1rem;
  justify-content: center;
  margin-top: 2rem;
}

.edit-btn, .logout-btn {
  padding: 0.8rem 1.5rem;
  border: none;
  border-radius: 25px;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.edit-btn {
  background: #f5e6d3;
  color: #6d4c3d;
}

.logout-btn {
  background: #f0e2d4;
  color: #8c7465;
}

.edit-btn:hover {
  background: #e8d5c4;
  transform: translateY(-2px);
}

.logout-btn:hover {
  background: #e0d1c1;
  transform: translateY(-2px);
}

@media (max-width: 768px) {
  .profile-card {
    padding: 1.5rem;
  }
  
  .info-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }

  .action-buttons {
    flex-direction: column;
  }
}
</style>